home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / trutil.c < prev    next >
C/C++ Source or Header  |  1993-08-16  |  7KB  |  417 lines

  1. #define YYSTYPE char *
  2. #include "asmtrans.h"
  3. #include "asmtab.h"
  4.  
  5. #define QUOTESYM '"'
  6. #define CMDSYM '%'
  7.  
  8. FILE *infile, *outfile;
  9. extern YYSTYPE yylval;
  10.  
  11. #define MAXNEST 10
  12. int hidecnt = 0;
  13. int ifstack[MAXNEST], ifstkptr;
  14.  
  15. void emit(s)
  16.     char *s;
  17. {
  18.     if (hidecnt == 0)
  19.         fputs(s, outfile);
  20.     free(s);
  21. }
  22.  
  23. char *concat(s1, s2)
  24.     char *s1, *s2;
  25. {
  26.     size_t siz = strlen(s1) + strlen(s2) + 1;
  27.     char *r;
  28.  
  29.     r = malloc(siz);
  30.     if (!r) return 0;
  31.  
  32.     strcpy(r, s1);
  33.     strcat(r, s2);
  34.     return r;
  35. }
  36.  
  37. char *concat3(s1, s2, s3)
  38.     char *s1, *s2, *s3;
  39. {
  40.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3) + 1;
  41.     char *r;
  42.  
  43.     r = malloc(siz);
  44.     if (!r) return 0;
  45.  
  46.     strcpy(r, s1);
  47.     strcat(r, s2);
  48.     strcat(r, s3);
  49.     return r;
  50. }
  51.  
  52. char *concat4(s1, s2, s3, s4)
  53.     char *s1, *s2, *s3, *s4;
  54. {
  55.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
  56.     char *r;
  57.  
  58.     r = malloc(siz);
  59.     if (!r) return 0;
  60.  
  61.     strcpy(r, s1);
  62.     strcat(r, s2);
  63.     strcat(r, s3);
  64.     strcat(r, s4);
  65.     return r;
  66. }
  67.  
  68. char *concat5(s1, s2, s3, s4, s5)
  69.     char *s1, *s2, *s3, *s4, *s5;
  70. {
  71.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
  72.             + strlen(s4) + strlen(s5) + 1;
  73.     char *r;
  74.  
  75.     r = malloc(siz);
  76.     if (!r) return 0;
  77.  
  78.     strcpy(r, s1);
  79.     strcat(r, s2);
  80.     strcat(r, s3);
  81.     strcat(r, s4);
  82.     strcat(r, s5);
  83.     return r;
  84. }
  85.  
  86. char *concat6(s1, s2, s3, s4, s5, s6)
  87.     char *s1, *s2, *s3, *s4, *s5, *s6;
  88. {
  89.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
  90.             + strlen(s4) + strlen(s5) + strlen(s6) + 1;
  91.     char *r;
  92.  
  93.     r = malloc(siz);
  94.     if (!r) return 0;
  95.  
  96.     strcpy(r, s1);
  97.     strcat(r, s2);
  98.     strcat(r, s3);
  99.     strcat(r, s4);
  100.     strcat(r, s5);
  101.     strcat(r, s6);
  102.     return r;
  103. }
  104.  
  105. char *concat8(s1, s2, s3, s4, s5, s6, s7, s8)
  106.     char *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8;
  107. {
  108.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
  109.             + strlen(s4) + strlen(s5) + strlen(s6) 
  110.             + strlen(s7) + strlen(s8) + 1;
  111.     char *r;
  112.  
  113.     r = malloc(siz);
  114.     if (!r) return 0;
  115.  
  116.     strcpy(r, s1);
  117.     strcat(r, s2);
  118.     strcat(r, s3);
  119.     strcat(r, s4);
  120.     strcat(r, s5);
  121.     strcat(r, s6);
  122.     strcat(r, s7);
  123.     strcat(r, s8);
  124.     return r;
  125. }
  126.  
  127. char *concat9(s1, s2, s3, s4, s5, s6, s7, s8, s9)
  128.     char *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
  129. {
  130.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
  131.             + strlen(s4) + strlen(s5) + strlen(s6) 
  132.             + strlen(s7) + strlen(s8) + strlen(s9) + 1;
  133.     char *r;
  134.  
  135.     r = malloc(siz);
  136.     if (!r) return 0;
  137.  
  138.     strcpy(r, s1);
  139.     strcat(r, s2);
  140.     strcat(r, s3);
  141.     strcat(r, s4);
  142.     strcat(r, s5);
  143.     strcat(r, s6);
  144.     strcat(r, s7);
  145.     strcat(r, s8);
  146.     strcat(r, s9);
  147.     return r;
  148. }
  149.  
  150.  
  151. static int is_word_sym(c)
  152.     int c;
  153. {
  154.     if (c == '.' || (c >= '0' && c <= '9')) return 1;
  155.     if (c >= 'a' && c <= 'z') return 1;
  156.     if (c >= 'A' && c <= 'Z') return 1;
  157.     if (c == '_') return 1;
  158.     return 0;
  159. }
  160.  
  161. typedef struct wordtab {
  162.     char *word;
  163.     char *defn;
  164.     struct wordtab *next;
  165. } WORDTAB;
  166.  
  167. WORDTAB *globaltab;
  168.  
  169. void
  170. do_define(word, defn)
  171.     char *word, *defn;
  172. {
  173.     WORDTAB *w;
  174.  
  175.     w = malloc(sizeof(WORDTAB));
  176.     if (!w) return;
  177.     w->word = strdup(word);
  178.     w->defn = strdup(defn);
  179.     w->next = globaltab;
  180.     globaltab = w;
  181. }
  182.  
  183. /*
  184.  * if we were actually using this program for
  185.  * large things, we would use a hash table
  186.  * to speed up the table lookup; but for the
  187.  * uses we have, there aren't likely to be
  188.  * many defines
  189.  */
  190.  
  191. char *
  192. wordlookup(which)
  193.     char *which;
  194. {
  195.     WORDTAB *w;
  196.  
  197.     for (w = globaltab; w; w = w->next) {
  198.         if (!strcmp(which, w->word)) {
  199.             return strdup(w->defn);
  200.         }
  201.     }
  202.     return strdup(which);
  203. }
  204.  
  205. void
  206. do_ifdef(which)
  207.     char *which;
  208. {
  209.     int output = 0;
  210.     WORDTAB *w;
  211.  
  212.     for (w = globaltab; w; w = w->next) {
  213.         if (!strcmp(which, w->word)) {
  214.             output = 1;
  215.             break;
  216.         }
  217.     }
  218.     if (ifstkptr < MAXNEST) {
  219.         ifstack[ifstkptr++] = output;
  220.         if (!output)
  221.             hidecnt++;
  222.     } else {
  223.         ifstkptr++;
  224.         yyerror("too many levels of %ifdef");
  225.     }
  226. }
  227.  
  228. void
  229. do_ifndef(which)
  230.     char *which;
  231. {
  232.     int output = 1;
  233.     WORDTAB *w;
  234.  
  235.     for (w = globaltab; w; w = w->next) {
  236.         if (!strcmp(which, w->word)) {
  237.             output = 0;
  238.             break;
  239.         }
  240.     }
  241.     if (ifstkptr < MAXNEST) {
  242.         ifstack[ifstkptr++] = output;
  243.         if (!output)
  244.             hidecnt++;
  245.     } else {
  246.         ifstkptr++;
  247.         yyerror("too many levels of %ifdef");
  248.     }
  249. }
  250.  
  251. void
  252. do_else()
  253. {
  254.     int output;
  255.  
  256.     if (ifstkptr == 0) {
  257.         yyerror("%else without %ifdef");
  258.     } else {
  259.         if (ifstkptr > MAXNEST) return;
  260.     /* if we were outputting, stop */
  261.     /* otherwise, start again */
  262.         output = !ifstack[ifstkptr-1];
  263.         if (output)
  264.             hidecnt--;
  265.         else
  266.             hidecnt++;
  267.         ifstack[ifstkptr-1] = output;
  268.     }
  269. }
  270.  
  271. void
  272. do_endif()
  273. {
  274.     int output;
  275.  
  276.     if (ifstkptr == 0) {
  277.         yyerror("%endif without %ifdef");
  278.     } else {
  279.         ifstkptr--;
  280.         if (ifstkptr >= MAXNEST) return;
  281.  
  282.         output = ifstack[ifstkptr];
  283.         if (!output)
  284.             hidecnt--;
  285.     }
  286. }
  287.  
  288. /* this is a terrible hack to remove the leading
  289.  * '_' from labels...
  290.  */
  291.  
  292. char *
  293. fixupword(w)
  294.     char *w;
  295. {
  296.     if (*w == '_' && syntax == PUREC)
  297.         return strdup(w+1);
  298.     else
  299.         return strdup(w);
  300. }
  301.  
  302.  
  303. static char footext[1024];
  304.  
  305. int
  306. yylex()
  307. {
  308.     int c;
  309.     char *to = footext;
  310.     int cmdword;
  311.     static int saweoln = 1;
  312.  
  313.     c = getc(infile);
  314.  
  315.     if (c < 0) {
  316. doeof:
  317.         saweoln = 1;
  318.         return 0;
  319.     }
  320.     if (c == ';') {
  321. docomment:
  322.         if (syntax == GAS)
  323.             c = '|';
  324.         *to++ = c;
  325.         do {
  326.             c = getc(infile);
  327.             if (c < 0) return 0;
  328.             if (c != '\r')
  329.                 *to++ = c;
  330.         } while (c != '\n');
  331.         *to = 0;
  332.         yylval = strdup(footext);
  333.         saweoln = 1;
  334.         return EOLN;
  335.     }
  336.     if (c == '\n') {
  337. doeoln:
  338.         *to++ = c;
  339.         *to = 0;
  340.         yylval = strdup(footext);
  341.         saweoln = 1;
  342.         return EOLN;
  343.     }
  344.     if (c == CMDSYM && saweoln) {
  345.         cmdword = 1;
  346.         c = getc(infile);
  347.     } else {
  348.         cmdword = 0;
  349.     }
  350.  
  351.     if (c == ' ' || c == '\t' || c == '\r') {
  352.         do {
  353.             if (c == '\r')
  354.                 c = ' ';
  355.             *to++ = c;
  356.             c = getc(infile);
  357.         } while (c == ' ' || c == '\t');
  358.         if (c == '\n') goto doeoln;
  359.         if (c == ';') goto docomment;
  360.         if (!cmdword) {
  361.             ungetc(c, infile);
  362.             *to = 0;
  363.             yylval = strdup(footext);
  364.             return WHITESP;
  365.         } else {
  366.             to = footext;
  367.         }
  368.     }
  369.  
  370.     saweoln = 0;
  371.  
  372.     if (c == QUOTESYM) {
  373.         for(;;) {
  374.             c = getc(infile);
  375.             if (c < 0) goto doeof;
  376.             if (c == QUOTESYM) break;
  377.             *to++ = c;
  378.         }
  379.         *to = 0;
  380.         yylval = strdup(footext);
  381.         return STRING;
  382.     }
  383.     if (is_word_sym(c)) {
  384.         do {
  385.             *to++ = c;
  386.             c = getc(infile);
  387.         } while (is_word_sym(c));
  388.         ungetc(c, infile);
  389.         *to = 0;
  390.         if (cmdword) {
  391.             yylval = footext;
  392.             if (!strcmp(footext, "define")) {
  393.                 return DEFINECMD;
  394.             } else if (!strcmp(footext, "include")) {
  395.                 return INCLUDECMD;
  396.             } else if (!strcmp(footext, "ifdef")) {
  397.                 return IFDEFCMD;
  398.             } else if (!strcmp(footext, "ifndef")) {
  399.                 return IFNDEFCMD;
  400.             } else if (!strcmp(footext, "else")) {
  401.                 return ELSECMD;
  402.             } else if (!strcmp(footext, "endif")) {
  403.                 return ENDIFCMD;
  404.             } else {
  405.                 fprintf(stderr, "Unknown command: %s\n", footext);
  406.             }
  407.         }
  408.         yylval = fixupword(footext);
  409.         return WORD;
  410.     }
  411.  
  412.     *to++ = c;
  413.     *to = 0;
  414.     yylval = footext;
  415.     return c;
  416. }
  417.